<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/loginbase.php' );

// --

class loginpegawai extends loginbase {

    //-- constructor
    public function __construct() {
        //-- parent constructor
        parent::__construct();
        //-- load library
        $this->load->library('tnotification');
         //--load model
        $this->load->model("user/m_user");
        $this->load->model("dokter/m_dokter");
        $this->load->model("pegawai/m_pegawai");
        $intId;
    }

    //-- view
    public function index() {
        // set template content
        $this->smarty->assign("strTemplateContent", "login/viewpegawai.html");
        //-- bisnis proses
        $id_user = $this->session->userdata('session_pegawai');
        if (!empty($id_user)) {
            $this->smarty->assign("strLoginSt", "true");
        }
        //-- url
        $this->smarty->assign("strUrlProcessLogin", site_url("login/loginpegawai/loginProcess"));
        $this->smarty->assign("strUrlLogout", site_url("login/loginpegawai/logoutProcess"));
        $this->smarty->assign("strUrlMenu", site_url("home/welcomepegawai/index"));
       
        //-- output
        parent::display();
    }

    //-- error
    public function error() {
        //-- set template content
        $this->smarty->assign("strTemplateContent", "login/errorpegawai.html");
        //-- bisnis proses
        //-- url
        $this->smarty->assign("strUrlLogin", site_url("login/loginpegawai/index"));
        $this->smarty->assign("strUrlProcessLogin", site_url("login/loginpegawai/loginProcess"));
        //-- output
        parent::display();
    }

    //-- login process
    public function loginProcess() {
        //-- set rules
      
        $this->tnotification->set_rules('txtUsername', 'Username', 'trim|required');
        $this->tnotification->set_rules('txtPassword', 'Password', 'trim|required');
        // process
        if ($this->tnotification->run() !== FALSE) {
            //-- params
            $strUsername = $this->input->post('txtUsername');
            $strPassword = $this->input->post('txtPassword');
            $strJabatan = $this->input->post('cb_jabatan');
            if ($strJabatan == 'Dokter') { 
                $strUserDokter = $this->m_dokter->getByUsername($strUsername);
                if ($strUserDokter['password'] == $strPassword) {
                    $arrUsersDokter = array("username" => $strUsername,
                        "jabatan" => $strJabatan,
                        "status" => TRUE);
                    $this->session->set_userdata("session_pegawai", $arrUsersDokter);
                    redirect('home/welcomepegawai/index'); 
                    
                } else {
                    
                    //-- output
                    redirect('login/loginpegawai/error');
                }
            }
            else if ($strJabatan == 'Pegawai Poli'){
                $strUserPegPoli = $this->m_pegawai->getByUsername($strUsername);
                if ($strUserPegPoli['password'] == $strPassword) {
                        $arrUsersPegPoli = array("username" => $strUsername,
                            "jabatan" => $strJabatan,
                            "status" => TRUE);
                        $this->session->set_userdata("session_pegawai", $arrUsersPegPoli);
                        redirect('home/welcomepegawai/index');

                } else {
                    //-- output
                    redirect('login/loginpegawai/error');
                }
            }
            else if ($strJabatan == 'Administarasi Obat'){
                $strUserAdObat = $this->m_pegawai->getByUsername($strUsername);
                if ($strUserAdObat['password'] == $strPassword) {
                        $arrUsersAdObat = array("username" => $strUsername,
                            "jabatan" => $strJabatan,
                            "status" => TRUE);
                        $this->session->set_userdata("session_pegawai", $arrUsersAdObat);
                        redirect('home/welcomepegawai/index');

                } else {
                    //-- output
                    redirect('login/loginpegawai/error');
                } 
            }
        } else {
            //-- default error
            redirect('login/loginpegawai/error');
        }
        //-- output
        redirect('login/loginpegawai/index');
    }

    //-- logout process
    public function logoutProcess() {
        //-- update logout data
        
        $this->session->unset_userdata('session_pegawai');
        redirect("login/loginpegawai/index");
    }

}